package com.binvshe.binvshe.mine.view;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.binvshe.binvshe.R;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/7/15
 */
public class CommentDialog extends DialogFragment implements View.OnClickListener {

    private CommentClickable mOnClickable;
    private EditText mEtComment;
    private TextView mTvTitle;
    private static final String KEY_TITLE = "key_title";
    private static final String KEY_HINT = "key_hint";
    private static final String KEY_LINES = "key_lins";
    private static final String KEY_FOUSE = "key_fouse";
    private String mTitle;
    private String mHint;
    private int mLines;
    private boolean mIsFouse;


    /**
     * @param title 标题栏文字
     * @param hint  EditText hint提示内容
     * @param lines EditText 行数
     * @return
     */
    public static CommentDialog newInstance(String title, String hint, int lines, boolean isFouse) {

        Bundle args = new Bundle();
        args.putString(KEY_TITLE, title);
        args.putString(KEY_HINT, hint);
        args.putInt(KEY_LINES, lines);
        args.putBoolean(KEY_FOUSE, isFouse);
        CommentDialog fragment = new CommentDialog();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        Window window = getDialog().getWindow();
        window.setWindowAnimations(R.style.AnimBottom); //设置窗口弹出动画
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_alert_comment, container);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setArguments();
        initView(view);
        return view;
    }

    private void setArguments() {
        mTitle = getArguments().getString(KEY_TITLE);
        mHint = getArguments().getString(KEY_HINT);
        mLines = getArguments().getInt(KEY_LINES);
        mLines = getArguments().getInt(KEY_LINES);
        mIsFouse = getArguments().getBoolean(KEY_FOUSE);
    }

    private void initView(View view) {
        View vwBlank = view.findViewById(R.id.mVwBlank);
        vwBlank.setOnClickListener(this);
        TextView tvEnter = (TextView) view.findViewById(R.id.mTvEnter);
        tvEnter.setOnClickListener(this);
        TextView tvCancel = (TextView) view.findViewById(R.id.mTvCancel);
        tvCancel.setOnClickListener(this);
        mTvTitle = (TextView) view.findViewById(R.id.mTvCommentTitle);
        mEtComment = (EditText) view.findViewById(R.id.mEtComment);
        mTvTitle.setText(mTitle);
        mEtComment.setHint(mHint);
        mEtComment.setLines(mLines);
        if (mIsFouse) {
            mEtComment.setFocusable(mIsFouse);
            getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }

    private String getCommentText() {
        return mEtComment.getText().toString().trim();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mVwBlank:
                dismiss();
                break;
            case R.id.mTvCancel:
                dismiss();
                break;
            case R.id.mTvEnter:
                mOnClickable.btnEnter(getCommentText());
                dismiss();
                break;
        }
    }

    public void setOnClickable(CommentClickable mOnClickable) {
        this.mOnClickable = mOnClickable;
    }

    public interface CommentClickable {
        void btnEnter(String comment);
    }
}
